<?php
use app\models\HomeDao;

$v_discountList = HomeDao::getFxDiscount();

?>

<script>
    //外部赋值
    var v_max_coin =<?=$user_info['DORACOIN_ACCOUNT']?>;
    var v_buy_result =<?=$user_info['BUY_RESULT']?>;
    var v_price =<?=$TOTALPRICE?>;
    var v_discountList =<?=json_encode($v_discountList)?>;


    //控件,coin总数，产品价格，折扣
    calc_coin = function (current_coin, totalPrice) {
        var me = $('#txt_duola_coin').get(0);
        if (typeof(totalPrice) != "undefined" && !isNaN(totalPrice)) {
            v_price = totalPrice;
        }
        var re = biz_calc_coin(current_coin, v_max_coin, v_price);
        me.value = re.current_coin;
        if ($('#exchange_money_dom').length >= 0)
            $('#exchange_money_dom').html("¥" + re.exchange_money);
        if ($('#realPrice_dom').length >= 0)
            $('#realPrice_dom').html(re.realPrice);

    }

    //输入coin,coin总数，产品价格，折扣
    biz_calc_coin = function (current_coin, max_coin, price) {

        if (current_coin == '') {
            current_coin = 0;
        }

        //不能超过最大值
        if (current_coin > max_coin) {
            current_coin = max_coin;
        }

        var exchange_money = current_coin * 100;

        //不能超过商品价格
        if (exchange_money > price * 100) {
            current_coin = parseInt(price);
            exchange_money = current_coin * 100;
        }
        //alert((1-discount));
        //debugger;
        var cashMoney = (price * 100 - exchange_money);
        var discount = 1;
        var buy_all_result = cashMoney / 100 + v_buy_result;


        $.each(v_discountList, function (i, item) {

            if (buy_all_result >= parseInt(item.BUYLOWER) && buy_all_result < parseInt(item.BUYUPPER)) {
                discount = item.DISCOUNT;

                return false;
            }
        });

        var realPrice = cashMoney * (100 - discount * 100) / 10000;
        exchange_money = exchange_money / 100;
        // alert(--buy_all_result+'-'+discount);
        return {
            current_coin: current_coin,
            exchange_money: exchange_money,
            realPrice: realPrice
        };


    }
    $(function ($) {
        calc_coin(9007199254740991);
    });
</script>